package others;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 学习Java反射API
 * @author rodz
 *
 */
public class MyClass implements java.io.Serializable,java.lang.Cloneable {
	int fieldA;
	String fieldB;
	
	public MyClass(){
		
	}
	
	public MyClass(int fieldA,String fieldB){
		this.fieldA = fieldA;
		this.fieldB = fieldB;
	}
	
	public void add(int a,int b){
		
	}
	
	public static void main(String[] args) {
		MyClass myClass = new MyClass();
		//输出类名(含包名)
		System.out.println(myClass.getClass().getName());
		//输出类名(不含包名)
		System.out.println(myClass.getClass().getSimpleName());
			
		
		
		try {
			Class<?> clazz = Class.forName("others.MyClass");
			
			//类的父类
			Class<?> superclass = clazz.getSuperclass();
			Object instance = clazz.newInstance();
			Constructor<?>[] constructors = clazz.getConstructors();
			for(Constructor constr:constructors){
				System.out.println(constr.toGenericString());
			}
			
			//类的实现接口
			Class<?>[] interfaces = clazz.getInterfaces();
			for(Class interFace:interfaces){
				System.out.println(interFace.getName());
			}
			
			//类的声明成员变量
			Field[] fields = clazz.getDeclaredFields();
			for(Field field:fields){
				field.setAccessible(true);
				System.out.println("成员变量"+field.getType().toString()+ " " +field.getName());
			}
			
			//类的声明方法
			Method[] declaredMethods = clazz.getDeclaredMethods();
			for(Method method:declaredMethods){
				System.out.println("Method:" + method.getName());
			}
			
			
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

